package com.controller;

import com.common.response.Result;
import com.repository.ComponentCatalogRepository;
import com.repository.ComponentRepository;
import com.request.ComponentRequest;
import com.request.ComponentCatalogRequest;
import com.response.ComponentCatalogResponse;
import com.response.ComponentResponse;
import com.service.ComponentCatalogService;
import com.service.ComponentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 组件接口
 */
@RestController
@RequestMapping("/component")
@Api(tags = "组件接口")
@Slf4j
public class ComponentController {
    @Autowired
    private ComponentRepository componentRepository;
    @Autowired
    private ComponentCatalogRepository componentCatalogRepository;
    @Autowired
    private ComponentService componentService;
    @Autowired
    private ComponentCatalogService componentCatalogService;

    /**
     * 组件的crud
     * @return
     */
    @PostMapping("/saveComponent")
    @ApiOperation(value = "组件 CRUD",response = ComponentResponse.class)
    public String saveComponent(@RequestBody ComponentRequest componentRequest){
        try {
            int i = 2/0;
            return Result.successObj( componentService.save(componentRequest));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }

    }

    /**
     * 组件目录crud
     * @return
     */
    @PostMapping("/saveComponentCatalog")
    @ApiOperation(value = "保存组件目录",response = ComponentCatalogResponse.class)
    public String saveComponentCatalog(@RequestBody ComponentCatalogRequest componentCatalogRequest){

        try {
            return Result.successObj(componentCatalogService.save(componentCatalogRequest));
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}
